package com.zhuhjay.eduorder.controller;


import com.zhuhjay.commonutils.R;
import com.zhuhjay.eduorder.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-09
 */
@Api(description = "订单支付日志控制器")
@RestController
@RequestMapping("/eduorder/paylog")

public class PayLogController {

    @Resource
    private PayLogService payLogService;

    @ApiOperation("生成微信支付二维码")
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map<String, Object> map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation("查询订单是否支付成功")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        // 查询订单支付状态
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (ObjectUtils.isEmpty(map)) {
            return R.error().message("订单支付失败");
        }
        // 判断支付是否成功
        if ("SUCCESS".equals(map.get("trade_state"))) {
            payLogService.updateOrderStates(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }


}

